package com.dews.web.controller.information;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dews.common.annotation.Log;
import com.dews.common.core.controller.BaseController;
import com.dews.common.core.domain.BaseResponse;
import com.dews.common.core.domain.TreeSelect;
import com.dews.common.core.domain.model.LoginUser;
import com.dews.common.core.page.TableDataInfo;
import com.dews.common.enums.BusinessType;
import com.dews.common.utils.SecurityUtils;
import com.dews.information.domain.dto.SubjectQueryDTO;
import com.dews.information.domain.dto.SubjectSaveDTO;
import com.dews.information.domain.dto.SubjectUpdateDTO;
import com.dews.information.domain.entity.Subject;
import com.dews.information.service.ISubjectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * 专业Controller
 *
 * @author qingfeng
 * @date 2024-11-15
 */
@RestController
@Validated
@RequestMapping("/information/subject")
public class SubjectController extends BaseController {

    private ISubjectService subjectService;

    @Autowired
    public SubjectController(ISubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 查询专业列表
     */
    @PreAuthorize("@ss.hasPermi('information:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(SubjectQueryDTO subjectQueryDTO) {
        return subjectService.selectSubjectList(subjectQueryDTO);
    }

    /**
     * 获取专业详细信息
     */
    @PreAuthorize("@ss.hasPermi('information:subject:query')")
    @GetMapping(value = "/{id}")
    public BaseResponse getInfo(@PathVariable("id") Integer id) {
        return success(subjectService.selectSubjectById(id));
    }

    /**
     * 新增专业
     */
    @PreAuthorize("@ss.hasPermi('information:subject:add')")
    @Log(title = "专业", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResponse add(@RequestBody @Validated SubjectSaveDTO subjectSaveDTO) {
        return toAjax(subjectService.insertSubject(subjectSaveDTO));
    }

    /**
     * 修改专业
     */
    @PreAuthorize("@ss.hasPermi('information:subject:edit')")
    @Log(title = "专业", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResponse edit(@RequestBody @Validated SubjectUpdateDTO subjectUpdateDTO) {
        return toAjax(subjectService.updateSubject(subjectUpdateDTO));
    }

    /**
     * 删除专业
     */
    @PreAuthorize("@ss.hasPermi('information:subject:remove')")
    @Log(title = "专业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public BaseResponse remove(@PathVariable List<Integer> ids) {
        return toAjax(subjectService.deleteSubjectByIds(ids));
    }

    /**
     * 根据学院ID查询专业信息表
     */
    @PostMapping("/depId")
    public BaseResponse<List<Subject>> getSubjectByDepId(@RequestBody Subject subject) {
        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<Subject>()
                .eq(ObjectUtil.isNotNull(subject.getDepId()), Subject::getDepId, subject.getDepId())
                .eq(Subject::getIsEnable, 1);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser.getUser().getRoles().stream().anyMatch(e -> e.getRoleKey().equals("subject"))) {
            queryWrapper.eq(Subject::getDirectorId, loginUser.getUserId());
        }
        return success(subjectService.list(queryWrapper));
    }

    /**
     * 根据登录人的学院ID查询专业列表
     * @return
     */
    @GetMapping("/byDepId")
    public BaseResponse<List<Subject>> getSubjectByLoginDepId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isNull(loginUser)) {
            return success(Collections.emptyList());
        }
        return success(subjectService.list(
                new LambdaQueryWrapper<Subject>()
                        .eq(Subject::getDepId, loginUser.getDeptId())
                        .eq(Subject::getIsEnable, 1)
        ));
    }

    @ApiOperation(value = "构建学院专业树")
    @GetMapping("/tree")
    public BaseResponse<List<TreeSelect>> getSubjectTree() {
        return success(subjectService.getSubjectTree());
    }

    @ApiOperation(value = "根据用户查询所在的专业列表")
    @GetMapping("/user")
    public BaseResponse<List<Subject>> getSubjectByUserId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isNull(loginUser)) {
            return success(Collections.emptyList());
        }
        return success(subjectService.list(
                new LambdaQueryWrapper<Subject>()
                        .eq(Subject::getDirectorId, loginUser.getUserId())
        ));
    }

}
